package com.tutu.service.impl;

import com.tutu.controller.CommonsController;
import com.tutu.core.Constant;
import com.tutu.core.api.AlarmMessageApiUrls;
import com.tutu.service.AlarmMessageService;
import com.tutu.utils.BaseUtil;
import com.tutu.utils.DateUtils;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.UnicodeUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * AlarmMessageServiceImpl
 * Created by 涂奕恒 on 2023/8/17 14:55.
 */
@Service
public class AlarmMessageServiceImpl implements AlarmMessageService {

    @Override
    public Map<String, Object> pageByParams(Integer companyId, String deviceCode, Integer type, String startTime, String endTime, String keyWord, Integer pageNum, Integer pageSize) {
        if (null == companyId) return BaseUtil.defaultPageErrorInfo();

        return HttpRequestUtil.getOnlyData(AlarmMessageApiUrls.base_prefix + AlarmMessageApiUrls.page, "companyId=" + companyId + "&keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize + "&deviceCode=" + deviceCode + "&startTime=" + DateUtils.stringToLongWithFormat(startTime, Constant.TIME_FORMAT_DEFAULT) + "&endTime=" + DateUtils.stringToLongWithFormat(endTime, Constant.TIME_FORMAT_DEFAULT) + "&type=" + (null == type ? Constant.TIME_DATA_TYPE : type));
    }

    @Override
    public Map<String, Object> syncData(HttpServletRequest request) {
        LogInfoServiceImpl.saveLogInfo(request, "同步预警消息");

        if (CommonsController.needSync("alarmMessage")) {
            return HttpRequestUtil.getSyncData(11);
        } else {
            return BaseUtil.defaultErrorInfo(null);
        }
    }

    @Override
    public Map<String, Object> iPageByParams(String openId, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(AlarmMessageApiUrls.base_prefix + AlarmMessageApiUrls.pageForWeb, "pageNum=" + pageNum + "&pageSize=" + pageSize + "&openId=" + openId + "&startTime=" + DateUtils.stringToLongWithFormat(startTime, Constant.TIME_FORMAT_DEFAULT) + "&endTime=" + DateUtils.stringToLongWithFormat(endTime, Constant.TIME_FORMAT_DEFAULT));
    }

    @Override
    public Map<String, Object> alarmMessageStatic(HttpServletRequest request, Integer count) {
        return HttpRequestUtil.getCompleteData(AlarmMessageApiUrls.base_prefix + AlarmMessageApiUrls.alarmMessageStatic, "count=" + count + "&companyId=" + request.getSession().getAttribute(Constant.CURRENT_LOGIN_COMPANY_ID));
    }
}